package com.charger.charger.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.chargerChargingCable.domain.ChargerChargingcable;
import com.charger.chargerChargingCable.service.ChargerChargingcableService;
import com.charger.chargingStationCharger.domain.ChargingstationCharger;
import com.charger.chargingStationCharger.service.ChargingstationChargerService;
import com.charger.chargingcable.domain.Chargingcable;
import com.charger.chargingcable.service.IChargingcableService;
import com.charger.fault.domain.Fault;
import com.charger.fault.service.IFaultService;
import com.charger.trade.domain.Records;
import com.charger.trade.service.IRecordsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.charger.common.annotation.Log;
import com.charger.common.core.controller.BaseController;
import com.charger.common.core.domain.AjaxResult;
import com.charger.common.enums.BusinessType;
import com.charger.charger.domain.Charger;
import com.charger.charger.service.IChargerService;
import com.charger.common.utils.poi.ExcelUtil;
import com.charger.common.core.page.TableDataInfo;

/**
 * 充电桩Controller
 * 
 * @author charger1
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/site/charger")
public class ChargerController extends BaseController
{
    @Autowired
    private IChargerService chargerService;

    @Autowired
    private ChargerChargingcableService chargerChargingcableService;

    @Autowired
    private IChargingcableService chargingcableService;

    @Autowired
    private IRecordsService chargingrecordsService;

    @Autowired
    private IFaultService failureService;

    @Autowired
    private ChargingstationChargerService chargingstationChargerService;

    /**
     * 查询充电桩列表
     */
    @PreAuthorize("@ss.hasPermi('site:charger:list')")
    @GetMapping("/list")
    public TableDataInfo list(Charger charger)
    {
        startPage();
        List<Charger> list = chargerService.selectChargerList(charger);
        return getDataTable(list);
    }

    /**
     * 查询未关联的充电桩列表
     */
    @PreAuthorize("@ss.hasPermi('site:charger:getUnrelatedCharger')")
    @GetMapping("/getUnrelatedCharger")
    public AjaxResult getUnrelatedChargerList()
    {
        startPage();
        List<Charger> list = chargerService.getUnrelatedChargerList();
        return AjaxResult.success(list);
    }

    /**
     * 导出充电桩列表
     */
    @PreAuthorize("@ss.hasPermi('site:charger:export')")
    @Log(title = "充电桩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Charger charger)
    {
        List<Charger> list = chargerService.selectChargerList(charger);
        ExcelUtil<Charger> util = new ExcelUtil<Charger>(Charger.class);
        util.exportExcel(response, list, "充电桩数据");
    }

    /**
     * 获取充电桩详细信息
     */
    @PreAuthorize("@ss.hasPermi('site:charger:query')")
    @GetMapping(value = "/{chargerId}")
    public AjaxResult getInfo(@PathVariable("chargerId") Long chargerId)
    {
        Charger charger=chargerService.selectChargerByChargerId(chargerId);
        ChargerChargingcable chargerChargingcable = new ChargerChargingcable();
        chargerChargingcable.setChargerId(charger.getChargerId());
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (chargerChargingcable.getChargerId() != null) {
            queryWrapper.eq("charger_id", chargerChargingcable.getChargerId());
        }
        List<ChargerChargingcable> list = chargerChargingcableService.list(queryWrapper);
        List<Chargingcable> chargingcables = new ArrayList<>();
        List<Long> chargingCableIds = new ArrayList<>();
        for (ChargerChargingcable item : list) {
            chargingCableIds.add(item.getChargingcableId());
            Chargingcable chargingcable = chargingcableService.selectChargingcableByChargingcableId(item.getChargingcableId());
            chargingcables.add(chargingcable);
        }
        charger.setChargingcableIds(chargingCableIds);
        charger.setChargingcables(chargingcables);

        Records records =new Records();
        records.setChargerId(chargerId);

        List<Records> list1=chargingrecordsService.selectChargingrecordsList(records);
        charger.setChargingrecords(list1);

        Fault fault =new Fault();
        fault.setChargerId(chargerId);
        List<Fault> list2=failureService.selectFailureList(fault);
        charger.setFailures(list2);
//            Lilysi到此一游
        return AjaxResult.success(charger);
    }

    /**
     * 新增充电桩
     */
    @PreAuthorize("@ss.hasPermi('site:charger:add')")
    @Log(title = "充电桩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Charger charger)
    {
        toAjax(chargerService.insertCharger(charger));
        Long id=charger.getChargerId();
        for (Long item:charger.getChargingcableIds()){
            ChargerChargingcable chargerChargingcable = new ChargerChargingcable();
            chargerChargingcable.setChargerId(id);
            chargerChargingcable.setChargingcableId(item);
            chargerChargingcableService.save(chargerChargingcable);
        }
        ChargingstationCharger chargingstationCharger=new ChargingstationCharger();
        chargingstationCharger.setChargingstationId(charger.getChargingstationId());
        chargingstationCharger.setChargerId(id);
        return success("添加成功！");
    }

    /**
     * 修改充电桩
     */
    @PreAuthorize("@ss.hasPermi('site:charger:edit')")
    @Log(title = "充电桩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Charger charger)
    {
        toAjax(chargerService.updateCharger(charger));
        Long id=charger.getChargerId();
        QueryWrapper<ChargerChargingcable> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("charger_id",id);
        chargerChargingcableService.remove(queryWrapper);
        chargingstationChargerService.removeById(id);
        ChargingstationCharger chargingstationCharger=new ChargingstationCharger();
        chargingstationCharger.setChargerId(id);
        chargingstationCharger.setChargingstationId(charger.getChargingstationId());
        chargingstationChargerService.save(chargingstationCharger);
        for(Long i:charger.getChargingcableIds()){
            ChargerChargingcable chargerChargingcable=new ChargerChargingcable();
            chargerChargingcable.setChargingcableId(i);
            chargerChargingcable.setChargerId(id);
            chargerChargingcableService.save(chargerChargingcable);
        }
        return success("修改成功！");
    }

    /**
     * 删除充电桩
     */
    @PreAuthorize("@ss.hasPermi('site:charger:remove')")
    @Log(title = "充电桩", businessType = BusinessType.DELETE)
	@DeleteMapping("/{chargerIds}")
    public AjaxResult remove(@PathVariable Long[] chargerIds)
    {
        return toAjax(chargerService.deleteChargerByChargerIds(chargerIds));
    }
}
